/*******************************************************************************
/
/ sentrada.cp
/
/ Creado por Roberto García Carvajal.
/
/ Sistema de entrada de datos por teclado y ratón
/
/ Es extremadamente sencillo.
/
/ Documento bajo licencia GPL.
/
*******************************************************************************/

#include <iostream>
#include <SDL/SDL.h>
#include "svideo.h"
#include "sentrada.h"
#include "evento.h"
#include "nucleo.h"

#define _M_E_DEBUG_ 0

char traductor(unsigned int a)
{
 switch(a)
 {
  case SDLK_0: return '0'; break;
  case SDLK_1: return '1'; break;
  case SDLK_2: return '2'; break;
  case SDLK_3: return '3'; break;
  case SDLK_4: return '4'; break;
  case SDLK_5: return '5'; break;
  case SDLK_6: return '6'; break;
  case SDLK_7: return '7'; break;
  case SDLK_8: return '8'; break;
  case SDLK_9: return '9'; break;
  case SDLK_KP0: return '0'; break;
  case SDLK_KP1: return '1'; break;
  case SDLK_KP2: return '2'; break;
  case SDLK_KP3: return '3'; break;
  case SDLK_KP4: return '4'; break;
  case SDLK_KP5: return '5'; break;
  case SDLK_KP6: return '6'; break;
  case SDLK_KP7: return '7'; break;
  case SDLK_KP8: return '8'; break;
  case SDLK_KP9: return '9'; break;
  case SDLK_COLON: return ':'; break;
  case SDLK_SEMICOLON: return ';'; break;
  case SDLK_PERIOD: return '.'; break;
 }
 if(a >= 97 && a <= 122)
 {
  char caracter = 'a';
  caracter = caracter + (a - 97);
  return caracter;
 }
 return ' ';
}

void c_sentrada::ciclo()
{
 SDL_Event evento;

 while(SDL_PollEvent(&evento))
 {
  switch(evento.type)
  {
   case SDL_QUIT:
   {
    c_evento ev(E_QUIT);
    nucleo->nuevo_evento(ev);
   } break;
   case SDL_MOUSEMOTION:
   {
    mousex = evento.motion.x;
    mousey = evento.motion.y;
   } break;
   case SDL_MOUSEBUTTONDOWN:
   {
    if(_M_E_DEBUG_)
     std::cout << "POSIBLE COLISION DETECTADA" << std::endl;
    if(click)
     svideo->colision(mousex,mousey);
    if(_M_E_DEBUG_)
     std::cout << "POSIBLE COLISION PROCESADA" << std::endl;
   } break;
   case SDL_MOUSEBUTTONUP:
   {
    if(_M_E_DEBUG_)
     std::cout << "DECOLISIONANDO" << std::endl;
    svideo->decolision();
   } break;
   case SDL_KEYDOWN:
   {
    if(evento.key.keysym.sym == SDLK_F1)
     svideo->pantallacompleta();
    else if(escritura)
    {
     if(evento.key.keysym.sym == SDLK_BACKSPACE)
      svideo->bcaracter();
     else
      svideo->acaracter(traductor(evento.key.keysym.sym));
    }
   } break;
   default: break;
  }
 }
 c_evento ev(E_MOVRATON);
 ev.vector[0] = mousex;
 ev.vector[1] = mousey;
 nucleo->nuevo_evento(ev);
}

